﻿'         This file is Copyright © 2008-2012 Billy O'Neal and Aommaster
'                            All rights reserved.
'
' This file is released under the 2-Clause BSD license, included in LICENSE.TXT
'                           and on the MHWT website
Imports System.Xml.Serialization
Imports System.IO
Imports System.Runtime.Serialization

<System.Serializable()> Friend Class Speech : Implements IComparable : Implements IEquatable(Of Speech)
    Private GuidInternal As Guid
    Private NameInternal As String = ""
    Private DescriptionInternal As String = ""
    Private LogsGeneratedInternal As String = ""
    Private TextInternal As String = ""
    Private SafeModeInternal As Boolean = False
    Public Delegate Sub Generic()
    <System.NonSerialized()> Private dNonSerializableHandlers As New List(Of Generic)
    Private dSerializableHandlers As New List(Of Generic)
    Public Custom Event Deleted As Generic
        AddHandler(ByVal value As Generic)
            If value.Target.GetType.IsSerializable Then
                dSerializableHandlers.Add(value)
            Else
                If dNonSerializableHandlers Is Nothing Then
                    dNonSerializableHandlers = New List(Of Generic)
                End If
                dNonSerializableHandlers.Add(value)
            End If
        End AddHandler
        RemoveHandler(ByVal value As Generic)
            If value.Target.GetType.IsSerializable Then
                dSerializableHandlers.Remove(value)
            Else
                dNonSerializableHandlers.Remove(value)
            End If
        End RemoveHandler
        RaiseEvent()
            Dim temp As New List(Of Generic)
            temp.AddRange(dNonSerializableHandlers)
            For Each item As Generic In temp
                item.Invoke()
            Next
            temp.Clear()
            temp.AddRange(dSerializableHandlers)
            For Each item As Generic In temp
                item.Invoke()
            Next
            temp.Clear()
        End RaiseEvent
    End Event
    <System.NonSerialized()> Private mNonSerializableHandlers As New List(Of Generic)
    Private mSerializableHandlers As New List(Of Generic)
    Public Custom Event Modified As Generic
        AddHandler(ByVal value As Generic)
            If value.Target.GetType.IsSerializable Then
                mSerializableHandlers.Add(value)
            Else
                If mNonSerializableHandlers Is Nothing Then
                    mNonSerializableHandlers = New List(Of Generic)
                End If
                mNonSerializableHandlers.Add(value)
            End If
        End AddHandler
        RemoveHandler(ByVal value As Generic)
            If value.Target.GetType.IsSerializable Then
                mSerializableHandlers.Remove(value)
            Else
                mNonSerializableHandlers.Remove(value)
            End If
        End RemoveHandler
        RaiseEvent()
            Dim temp As New List(Of Generic)
            temp.AddRange(mNonSerializableHandlers)
            For Each item As Generic In temp
                item.Invoke()
            Next
            temp.Clear()
            temp.AddRange(mSerializableHandlers)
            For Each item As Generic In temp
                item.Invoke()
            Next
            temp.Clear()
        End RaiseEvent
    End Event
    Public ReadOnly Property GUID() As Guid
        Get
            Return GuidInternal
        End Get
    End Property
    Public Property name() As String
        Get
            Return NameInternal
        End Get
        Set(ByVal value As String)
            If value <> NameInternal Then
                NameInternal = value
                RaiseEvent Modified()
            End If
        End Set
    End Property
    Public Property Description() As String
        Get
            Return DescriptionInternal
        End Get
        Set(ByVal value As String)
            If value <> DescriptionInternal Then
                DescriptionInternal = value
                RaiseEvent Modified()
            End If
        End Set
    End Property
    Public Property LogsGenerated() As String
        Get
            Return LogsGeneratedInternal
        End Get
        Set(ByVal value As String)
            If value <> LogsGeneratedInternal Then
                LogsGeneratedInternal = value
                RaiseEvent Modified()
            End If
        End Set
    End Property
    Public Property SafeMode() As Boolean
        Get
            Return SafeModeInternal
        End Get
        Set(ByVal value As Boolean)
            If value <> SafeModeInternal Then
                SafeModeInternal = value
                RaiseEvent Modified()
            End If
        End Set
    End Property
    Public Property Text() As String
        Get
            Return TextInternal
        End Get
        Set(ByVal value As String)
            If TextInternal <> value Then
                TextInternal = value
                RaiseEvent Modified()
            End If
        End Set
    End Property
    Public Sub delete()
        RaiseEvent Deleted()
        Return
    End Sub
    Public Function save() As Boolean
        Dim dlg As New SaveFileDialog
        dlg.Filter = "Canned Speeches (*.can)|*.can|XML Files (*.xml)|*.xml|Text Files (*.txt)|*.txt"
        If dlg.ShowDialog() = DialogResult.OK Then
            Me.save(dlg.FileName)
            Return True
        Else
            Return False
        End If
    End Function
    Public Sub save(ByVal fileName As String)
        Dim xs As New XmlSerializer(GetType(Speech))
        Dim sw As New System.Xml.XmlTextWriter(New StreamWriter(New FileStream(fileName, FileMode.Create)))
        xs.Serialize(sw, Me)
        sw.Close()
    End Sub
    Public Sub load(ByVal fileName As String)
        Dim xs As New XmlSerializer(GetType(Speech))
        Dim sw As New System.Xml.XmlTextReader(New FileStream(fileName, FileMode.Open, FileAccess.Read))
        Dim tempO As New Speech
        tempO = DirectCast(xs.Deserialize(sw), Speech)
        Me.GuidInternal = tempO.GUID
        Me.name = tempO.name
        Me.Description = tempO.Description
        Me.LogsGenerated = tempO.LogsGenerated
        Me.Text = tempO.Text
        Me.SafeMode = tempO.SafeMode
        sw.Close()
    End Sub
    Public Overrides Function ToString() As String
        Return name
    End Function
    Public Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo
        If obj.GetType() Is GetType(Speech) Then
            Dim sph As Speech = DirectCast(obj, Speech)
            Return name.CompareTo(sph.name)
        Else
            Throw New ArgumentException("There was an error in sorting: A speech tried to compare to a non speech.")
        End If
    End Function
    Function EqualCompare(ByVal other As Speech) As Boolean Implements IEquatable(Of Speech).Equals
        If GUID = other.GUID Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Sub New()
        GuidInternal = System.Guid.NewGuid
    End Sub
    <OnDeserialized()> Private Sub OnDeSerialized(ByVal context As StreamingContext)
        dNonSerializableHandlers = New List(Of Generic)
        dSerializableHandlers = New List(Of Generic)
        mNonSerializableHandlers = New List(Of Generic)
        mSerializableHandlers = New List(Of Generic)
    End Sub
End Class